/*
 * GenerateDAOImpl.java		1.00	2008. 01. 21
 *
 * Copyright (c) 2008 David.Kim(KJS). All Rights Reserved.
 * 
 * This software is the confidential and proprietary information
 * of David.Kim(KJS).  You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license agreement
 * you entered into with David.Kim(KJS).
 */
package doit.gen.util;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.List;
import java.util.Map;

import doit.framework.util.Config;
import doit.gen.domain.Column;

/**
 * <pre>
 * system      : 공통
 * menu        : 공통
 * source      : GenerateDAOImpl.java
 * description : 테이블에 매핑 되는 DAOImpl 생성
 * </pre> 
 * @version
 * <pre> 
 * 1.0	2008. 01. 21  leewan  최초 작성
 * 1.1	
 * </pre>
 */

@SuppressWarnings("unchecked")
public class GenerateDAOImpl implements BaseGenerate {
	
	private String auth = "";
	
	public void generate( Map input, String tableName, List columns) throws Exception {
		String packageName = (String) input.get("packageName");
		String auth = (String) input.get("auth");
		String systemDiv = (String) input.get("systemDiv");
		String basePath = Config.getString("doit.generator.path.base");
        String parentPath = basePath + "/" + packageName;
		String childPath = Config.getString("doit.generator.path.daoImpl");
		String tableComment = columns.size() > 0 ? ((Column)columns.get(0)).getTableComments() : "";
		
		if (auth.equals("usr"))
			this.auth = auth;
		
		// 폴더 생성
		GeneratorUtil.makeDirectorys( basePath, packageName);
		
		// 폴더 생성
		GeneratorUtil.makeDirectorys( parentPath, childPath);

		// 클래스명 분석
		String className = GeneratorUtil.getClassName( tableName);
		
		// 내용 생성
		StringBuffer sb = new StringBuffer();
		
		// 라이선스 생성
		sb.append( GeneratorUtil.printLicense(className + "DAOImpl"));
		
		// 패키지 생성
		sb.append( GeneratorUtil.printPackage(packageName + "." + childPath.replace("/" ,".")));
		
		// 임포트 생성
		sb.append( printImport(packageName, className));
		
		// 자바독 생성
		sb.append( GeneratorUtil.printJavaDoc(className + "DAOImpl", systemDiv, tableComment, tableComment));
		
		// 클래스 시작
		sb.append( printClassStart(className));
		
		// 클래스
		sb.append( printClass(className, columns));
		
		// 클래스 끝
		sb.append( printClassEnd());
		
		// 파일 생성
		File file = GeneratorUtil.makeFile( parentPath + "/" + childPath + "/" + className + "DAOImpl" + "." + "java");
		
		// 파일에 쓰기
		RandomAccessFile raf = new RandomAccessFile( file, "rw");
		GeneratorUtil.write( raf, sb.toString());
		
		// 파일 닫기
		raf.close();
	}

    public String printImport(String packageName, String className) {
        StringBuffer sb = new StringBuffer();

        sb.append("import java.util.List;").append("\n");
        sb.append("import java.util.HashMap;").append("\n");
        sb.append("import java.util.Map;").append("\n");
        sb.append("\n");
        sb.append("import org.springframework.dao.DataAccessException;").append("\n");
        sb.append("import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;").append("\n");
        sb.append("\n");
        sb.append("import doit.framework.util.PageUtils;").append("\n");
        sb.append("import doit.framework.util.SharedMethods;").append("\n");
        sb.append("\n");
        sb.append("import " + packageName + ".dao."+className+"DAO;").append("\n");
        sb.append("import " + packageName + ".domain."+className+";").append("\n");
        sb.append("\n");
        
        return sb.toString();
    }	
	
	public String printClass(String className, List columns) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer();
		
		sb.append("    public " + className + "DAOImpl() {").append("\n");
		sb.append("    }").append("\n\n");
		sb.append(getPageList(className, columns));
		sb.append(getList(className, columns));
		sb.append(get(className, columns));
		sb.append(insert(className, columns));
		sb.append(update(className, columns));
		sb.append(updateField(className, columns));
		sb.append(delete(className, columns));
		sb.append(deleteAll(className, columns));
		sb.append(exist(className, columns));

		return sb.toString();
	}

	public String getPageList(String className, List columns) throws Exception {
		StringBuffer sb = new StringBuffer();
		
		sb.append("    public Map get" + className +  "PageList( Map input) throws DataAccessException {").append("\n");
		sb.append("        Map output = new HashMap();").append("\n");
		sb.append("        output.put( \"count\", getSqlMapClientTemplate().queryForObject(\"" + auth + className + ".get" + className + "PageListCount\", input));").append("\n");
		sb.append("        PageUtils.adjustPage( input, output);").append("\n");
		sb.append("        List list = getSqlMapClientTemplate().queryForList(\"" + auth + className + ".get" + className + "PageList\", input);").append("\n");
		sb.append("        SharedMethods.bindDAOObject( list);").append("\n");
		sb.append("        output.put( \"output\",  list);").append("\n");
		sb.append("        return output;").append("\n");
		sb.append("    }").append("\n\n");
		
		return sb.toString();
	}
		
	public String getList(String className, List columns) throws Exception {
		StringBuffer sb = new StringBuffer();
		
		sb.append("    public List get" + className +  "List( Map input) throws DataAccessException {").append("\n");
		sb.append("        List list = getSqlMapClientTemplate().queryForList(\"" + auth + className + ".get" + className + "List\", input);").append("\n");
		sb.append("        SharedMethods.bindDAOObject(list);").append("\n");
		sb.append("        return list;").append("\n");
		sb.append("    }").append("\n\n");
		
		return sb.toString();
	}
		
	public String get(String className, List columns) throws Exception {
		StringBuffer sb = new StringBuffer();
		
        sb.append("    public Map get" + className +  "( Map input) throws DataAccessException {").append("\n");
        sb.append("        Map output = (Map)getSqlMapClientTemplate().queryForObject(\"" + auth + className + ".get" + className + "\", input);").append("\n");
        sb.append("        SharedMethods.bindDAOObject( output);").append("\n");
        sb.append("        return output;").append("\n");
        sb.append("    }").append("\n\n");
		
		return sb.toString();
	}

	public String insert(String className, List columns) throws Exception {
        StringBuffer sb = new StringBuffer();
        
        sb.append("    public Object insert" + className +  "( " + className + " input) throws DataAccessException {").append("\n");
        sb.append("        return getSqlMapClientTemplate().insert(\"" + auth + className + ".insert" + className + "\", input);").append("\n");
        sb.append("    }").append("\n\n");
        
        return sb.toString();
    }
    
    public String update(String className, List columns) throws Exception {
        StringBuffer sb = new StringBuffer();

        sb.append("    public int update" + className +  "( " + className + " input) throws DataAccessException {").append("\n");
        sb.append("        return getSqlMapClientTemplate().update(\"" + auth + className + ".update" + className + "\", input);").append("\n");
        sb.append("    }").append("\n\n");

        return sb.toString();
    }
    
    public String updateField(String className, List columns) throws Exception {
        StringBuffer sb = new StringBuffer();
        
        sb.append("    public int updateField" + className +  "( " + className + " input) throws DataAccessException {").append("\n");
        sb.append("        return getSqlMapClientTemplate().update(\"" + auth + className + ".updateField" + className + "\", input);").append("\n");
        sb.append("    }").append("\n\n");
        
        return sb.toString();
    }
    
    public String delete(String className, List columns) throws Exception {
        StringBuffer sb = new StringBuffer();

        sb.append("    public int delete" + className +  "( " + className + " input) throws DataAccessException {").append("\n");
        sb.append("        return getSqlMapClientTemplate().delete(\"" + auth + className + ".delete" + className + "\", input);").append("\n");
        sb.append("    }").append("\n\n");

        return sb.toString();
    }
    
    public String deleteAll(String className, List columns) throws Exception {
        StringBuffer sb = new StringBuffer();
        
        sb.append("    public int delete" + className +  "All( Map input) throws DataAccessException {").append("\n");
        sb.append("        return getSqlMapClientTemplate().delete(\"" + auth + className + ".delete" + className + "All\", input);").append("\n");
        sb.append("    }").append("\n\n");
        
        return sb.toString();
    }
    
    public String exist(String className, List columns) throws Exception {
        StringBuffer sb = new StringBuffer();

        sb.append("    public Object exist" + className +  "( Map input) throws DataAccessException {").append("\n");
        sb.append("        return getSqlMapClientTemplate().queryForObject(\"" + auth + className + ".exist" + className + "\", input);").append("\n");
        sb.append("    }").append("\n");
        
        return sb.toString();
    }
	
	public String printField(Column column) throws Exception {
		return null;
	}

	public String printClassStart( String className) {
		return "public class " + className + "DAOImpl extends SqlMapClientDaoSupport implements " + className + "DAO" +  " {\n";
	}
	
	public String printClassEnd() {
		return "}\n";
	}
}
